package thread;

public class SyncDemo6 {
    public static void main(String[] args) {
        Foo foo=new Foo();
        Thread t1=new Thread(){
            public void run(){
                foo.method1();
            }
        };
        Thread t2=new Thread(foo::method2);
        t1.start();
        t2.start();
    }


}

 class Foo{
    public synchronized void method1(){
        try {
            Thread t=Thread.currentThread();
            System.out.println(t.getName()+"开始执行");
            Thread.sleep(1000);
            System.out.println(t.getName()+"执行结束");
        } catch (InterruptedException e) {
        }
    }
    public synchronized void method2(){
        try {
            Thread t=Thread.currentThread();
            System.out.println(t.getName()+"开始执行");
            Thread.sleep(1000);
            System.out.println(t.getName()+"执行结束");
        } catch (InterruptedException e) {
        }
    }
}
